#include <bits/stdc++.h>
// 2025/02/01
// tag: 
// Author: Zhang Muen
using namespace std;

int n, m, Map[5002][5002];

void init(){
    for (int i = 1; i <= 5001; i++) {
        for (int j = 1; j <= 5001; j++) {
            Map[i][j] += Map[i][j - 1] + Map[i - 1][j] - Map[i - 1][j - 1];
        }
    }
}

int presum(int x, int y, int x1, int y1){
    return Map[x1][y1] - Map[x1][y] - Map[x][y1] + Map[x][y];
}

signed main()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        int x, y, v;
        cin >> x >> y >> v;
        Map[x + 1][y + 1] += v;
    }
    init();
    int ans = 0;
    for (int i = m; i <= 5001; i++){
        for (int j = m; j <= 5001; j++){
            ans = max(ans, presum(i, j, i - m, j - m));
        }
    }
    cout << ans << endl;
    return 0;
}